package im

import (
	"encoding/json"
	"dhfshop/controllers/base"
	"thirdpaysdk/api/megaezgo"
	"github.com/astaxie/beego"
	"dhfshop/domain/im"
	"dhfshop/domain/pay"
	"dhfshop/service/redisgo"
)

type MegaezgoController struct {
	base.BaseController
}

//wisecashier异步回调
func (this *MegaezgoController) MegaezgoNotify() {
	notifyData := this.Ctx.Input.RequestBody
	beego.Debug("MegaezgoNotify notifyData:", string(notifyData))
	var response megaezgo.MegaezgoMsg
	message := string(notifyData)
	err := json.Unmarshal([]byte(message), &response)
	if err != nil {
		beego.Debug("MegaezgoNotify:", err.Error())
		return
	}
	var payInfoCache *paylayer.PayInfoCache = new(paylayer.PayInfoCache)
	cacheKey := "payorder_" + response.STORE_ORDER_NO
	redisconn := redisgo.GetInstance()
	if cacheValue, err := redisconn.GetString(cacheKey); err == nil && len(cacheValue) > 5  {
	    //beego.Debug("MegaezgoNotify cache pay info:", cacheValue)
		if err = json.Unmarshal([]byte(cacheValue), payInfoCache); err == nil {
			
		}
	}else{
	    beego.Debug("MegaezgoNotify cache pay info err:", err.Error())
		return 
	}
	reqMap := megaezgo.MegaezgoMsgNotify(&response)
	reqMap["LocalKind"] = "MEGAEZGO_NOTIFY"
	reqMap["attach"] = payInfoCache.OrderKind
	reqMap["currency"] = response.ORDER_CURRENCY
	imlayer.PushMsgSvrIns.PushDao <- &reqMap
	this.Ctx.WriteString("success")
	return
}
